package io.yyxx.boot.business.service.impl;

import cn.hutool.json.JSONUtil;
import io.yyxx.boot.business.entity.billDataEntity.CarBillData;
import io.yyxx.boot.business.vo.CarBillDataResponseVo;
import io.yyxx.boot.util.DateTimeToTimestampUtil;
import io.yyxx.boot.util.SaveDataToYiDa;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class Car {

    @Resource
    private SaveDataToYiDa saveDataToYiDa;

    @Resource
    private CarBillDataServiceImpl carBillDataServiceImpl;

    public void saveTrainBillToYiDa() {
        List<CarBillDataResponseVo> responseVoToYiDa;
        //从数据库中查询航班账单数据
        List<CarBillData> carBillData = carBillDataServiceImpl.selectCarBillData();

        // 将筛选后的数据映射到 ResponseVo 对象中，并拼接时间字符串
        responseVoToYiDa = carBillData.stream()
                .map(data -> {
                    CarBillDataResponseVo vo = new CarBillDataResponseVo();
                    ArrayList<String> travelerId = new ArrayList<>();
                    ArrayList<String> dept = new ArrayList<>();
                    ArrayList<String> time = new ArrayList<>();

                    // 拼接出发时间（deptDate + deptTime）
                    String startTime = "";
                    if (data.getDeptDate() != null && data.getDeptTime() != null) {
                        startTime = data.getDeptDate() + " " + data.getDeptTime();
                    }

                    // 拼接到达时间（arrDate + arrTime）
                    String endTime = "";
                    if (data.getArrDate() != null && data.getArrTime() != null) {
                        endTime = data.getArrDate() + " " + data.getArrTime();
                    }

                    //其他不用拼接的变量,将时间转为时间戳
                    String startTimestamp = DateTimeToTimestampUtil.DateTimeToTimestamp2(startTime) + "";
                    String endTimestamp = DateTimeToTimestampUtil.DateTimeToTimestamp2(endTime) + "";
                    time.add(startTimestamp);
                    time.add(endTimestamp);
                    vo.setTime(time);                                //时间区间

                    travelerId.add(data.getTravelerId());
                    vo.setTravelerId(travelerId);                 //  出差人id
                    dept.add(data.getDepartmentId());
                    vo.setPaymentDepartmentId(dept);            // 费用归属部门id
                    vo.setSettlementFee(data.getSettlementFee());  // 用车结算金额
                    String bookTime = DateTimeToTimestampUtil.DateTimeToTimestamp2(data.getBookTime()) + "";
                    vo.setBookTime(bookTime);                   // 预订时间
                    vo.setProjectName(data.getProjectName());   // 项目名称
                    vo.setProjectCode(data.getProjectCode());   // 项目编码
                    vo.setApplyId(data.getApplyId());           // 审批单号
                    vo.setCostCenter(data.getCostCenter());    // 成本中心
                    vo.setCostCenterNumber(data.getCostCenterNumber());// 成本中心编码

                    vo.setCar_level(data.getCarLevel());        // 用车级别(车型)
                    vo.setRealFromAddr(data.getRealFromAddr()); // 出发地
                    vo.setRealToAddr(data.getRealToAddr());     // 目的地
                    vo.setState("未报销");                        //设置状态

                    vo.setFeeTypeDesc(data.getFeeTypeDesc());// 费用类型描述

                    return vo;
                })
                .collect(Collectors.toList());

        for (CarBillDataResponseVo carBillDataResponseVo : responseVoToYiDa) {
            //转成josn格式
            String jsonStr = JSONUtil.toJsonStr(carBillDataResponseVo);
            //调用SendData发送至宜搭
            try {
                saveDataToYiDa.saveData(jsonStr);
                log.info("用车账单数据jsonStr:" + jsonStr);
                log.info("用车账单数据发送成功");
            } catch (Exception e) {
                log.error("用车账单数据向宜搭发送失败" + e);
                throw new RuntimeException(e);
            }
        }

    }
}
